package com.yequan.hbb.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.entries.AnswerEntries;
import com.yequan.hbb.entries.DynamicEntries;
import com.yequan.hbb.view.Question2Activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by DingLong on 2016/10/13.
 */

public class DynamicAdapter extends CommonAdapter<Object,DynamicAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<DynamicEntries.DataBean.ContentBean.DataBean2> list;
    private int contentId;

    public DynamicAdapter(Context context) {
        super(context, new int[]{R.layout.item_dynamic_list,R.layout.item_question2_answerlist});
        this.context = context;
    }

    @Override
    public void onBindView(ViewHolder holder, Object data) {
        if (data instanceof DynamicEntries.DataBean.ContentBean.DataBean2) {
            DynamicEntries.DataBean.ContentBean.DataBean2 dataBean = (DynamicEntries.DataBean.ContentBean.DataBean2) data;
            holder.dynamic_answernum.setText(dataBean.getAnswerNum()+"\r\n"+"回答");
            holder.dynamic_clientname.setText(dataBean.getClientName());
            String popularity = dataBean.getPopularity() + " 声望";
            String createTime = dataBean.getCreateTime();
            String modifyTime = dataBean.getModifyTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CANADA);
            try {
                Date parse = dateFormat.parse(modifyTime);
                long time = parse.getTime();
                String spaceTime = getSpaceTime(time);
                if (dataBean.getAnswerNum() == 0) {
                    spaceTime= spaceTime + "提问";
                } else {
                    spaceTime = spaceTime + "回答";
                }
                holder.dynamic_popularity_modifyTime.setText(popularity+" "+spaceTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            holder.dynamic_content.setText(dataBean.getTitle());
            contentId = dataBean.getContentId();
            holder.dynamic_content.setOnClickListener(this);
            holder.dynamic_img.setImageURI(Uri.parse(dataBean.getClientImage()));

        } else if (data instanceof AnswerEntries.DataBean.ContentBean.AnswerBean.DataBean2) {
            AnswerEntries.DataBean.ContentBean.AnswerBean.DataBean2 dataBean2 = (AnswerEntries.DataBean.ContentBean.AnswerBean.DataBean2) data;
            holder.answer_username.setText(dataBean2.getClientName());
            holder.answer_popularity.setText("声望 " + dataBean2.getPopularity());
            holder.answer_content.setText(dataBean2.getCoverSummary());
            String modifyTime = dataBean2.getModifyTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CANADA);
            try {
                Date parse = dateFormat.parse(modifyTime);
                long time = parse.getTime();
                String spaceTime = getSpaceTime(time);
                holder.answer_time.setText(spaceTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            holder.answer_favor.setText("" + dataBean2.getLikeNum());
            holder.answer_unfavor.setText(dataBean2.getHateNum() + "");
            holder.answer_unfavor.setText(dataBean2.getCommentNum()+"");

        }
    }

    @Override
    public int getItemViewType(int position) {
        Object item = getItem(position);
        if (item instanceof DynamicEntries.DataBean.ContentBean.DataBean2) {
            return 0;
        } else if (item instanceof AnswerEntries.DataBean.ContentBean.AnswerBean.DataBean2) {
            return 1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, Question2Activity.class);
        intent.putExtra("contentId", contentId);
        context.startActivity(intent);
    }

    public static class ViewHolder extends CommonAdapter.ViewHolder{

        private  TextView dynamic_answernum;
        private  TextView dynamic_clientname;
        private  TextView dynamic_popularity_modifyTime;
        private  ImageView dynamic_img;
        private  TextView dynamic_content;
        private  ImageView answer_usericon;
        private  TextView answer_username;
        private  TextView answer_popularity;
        private  TextView answer_content;
        private  TextView answer_time;
        private  TextView answer_favor;
        private  TextView answer_unfavor;
        private  TextView answer_commend;

        public ViewHolder(View itemView) {
            super(itemView);
            //动态的
            dynamic_answernum = ((TextView) itemView.findViewById(R.id.dynamic_answernum));
            dynamic_clientname = ((TextView) itemView.findViewById(R.id.dynamic_clientname));
            dynamic_popularity_modifyTime = ((TextView) itemView.findViewById(R.id.dynamic_popularity_modifyTime));
            dynamic_img = ((ImageView) itemView.findViewById(R.id.dynamic_img));
            dynamic_content = ((TextView) itemView.findViewById(R.id.dynamic_content));
            //二级界面的
            answer_usericon = ((ImageView) itemView.findViewById(R.id.answer_usericon));
            answer_username = ((TextView) itemView.findViewById(R.id.answer_username));
            answer_popularity = ((TextView) itemView.findViewById(R.id.answer_popularity));
            answer_content = ((TextView) itemView.findViewById(R.id.answer_content));
            answer_time = ((TextView) itemView.findViewById(R.id.answer_time));
            answer_favor = ((TextView) itemView.findViewById(R.id.answer_favor));
            answer_unfavor = ((TextView) itemView.findViewById(R.id.answer_unfavor));
            answer_commend = ((TextView) itemView.findViewById(R.id.answer_commend));

        }
    }
    /**
     * 获取时间间隔
     *
     * @param millisecond
     * @return
     */
    public static String getSpaceTime(Long millisecond) {
        Long currentMillisecond = System.currentTimeMillis();

        //间隔秒
        Long spaceSecond = (currentMillisecond - millisecond) / 1000;

        //一分钟之内
        if (spaceSecond >= 0 && spaceSecond < 60) {
            return spaceSecond+"秒之前";
        }
        //一小时之内
        else if (spaceSecond / 60 > 0 && spaceSecond / 60 < 60) {
            return spaceSecond / 60 + "分钟之前";
        }
        //一天之内
        else if (spaceSecond / (60 * 60) > 0 && spaceSecond / (60 * 60) < 24) {
            return spaceSecond / (60 * 60) + "小时之前";
        }
        //3天之内
        else if (spaceSecond/(60*60*24)>0){
            return spaceSecond/(60*60*24)+"天之前";
        }
        else return  spaceSecond/(60*60*24)+"天之前";
    }
}
